const express = require('express');
const router = express.Router();
const _ = require('lodash');
const config = require('../config/env/config_development.json');
const merchantFacade = require('../facade/merchant.server.facade');

const excudeUrl = [
    "/api/merchants",
    "/api/merchants/re/register",
    "/api/merchants/status",
    "/api/merchants/logout",
    "/api/merchants/login",
    "/api/merchants/register"
];
router.all("*",function(req,res,next){
    console.log("requestUrl:"+req.originalUrl);
    if(_.indexOf(excudeUrl,req.originalUrl)!=-1){
        next();
    }else{
        console.log(req.originalUrl);

       let merchantId = req.merchantId;

        merchantFacade.findMerchantStatusById(merchantId,function (error,doc) {
            if (error){
                next(new Error(error));
            }else{
                if (doc.mStatus=='white_list'){
                    next();
                }else{
                    res.json({
                        code:-2,
                        message:"you have no permission"
                    });
                }
            }
        })

    }

});
module.exports =router;